package com.example.administrator.dc.dialog;

import android.app.Dialog;

import android.app.ProgressDialog;
import android.content.Context;

import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;

import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.dc.R;

import java.util.Random;

/**
 * Created by Administrator on 2017/7/3.
 */

public class coustomDialog extends Dialog{
private  static  int defaultWidth=200;
    private  static  int getdefaultHeihgt=160;
    private  int  progress=0;
    public ProgressDialog dialog;
    private TextView dialogText;


    public coustomDialog(Context context){
        super(context);
    }


    public  float getDensity (Context context){
        Resources res =context.getResources();
        DisplayMetrics dm =res.getDisplayMetrics();
        return dm.density;
    }

    public  coustomDialog(Context context ,int width ,int height,int layout ,int style){
        super(context, style);
        setContentView(layout);
        //设置内容
        Window window=getWindow();
        WindowManager.LayoutParams params=window.getAttributes();
        //设置宽度高度。密度。。对齐方式。
        float density =getDensity(context);
        params.width=(int)(width*density);
        params.height=(int)(height*density);
        params.gravity= Gravity.CENTER;
        window.setAttributes(params);
        dialog = ProgressDialog.show(context, "加載中...", "正在加载。。。。，請稍後！");
        dialogText= (TextView) findViewById(R.id.coustom);
        new Thread(new TimeThread()).start();

    }
    final Handler  timeHandler  = new Handler(){
        public void handleMessage(Message msg){
            switch(msg.what){
                case 1:
                    if (progress<100){
                        progress=progress+new Random().nextInt(100);
                        if (progress<=100){
                            dialogText.setText("更新进度"+progress+"%");
                        }else {

                            dialogText.setText("更新进度100%");
                        }
                    }
                    else{
                        //Toast.makeText(getContext(),"加载完成",Toast.LENGTH_LONG).show();
                        dialog.dismiss();
                        break;
                    }
            }
            super.handleMessage(msg);
        }
    } ;

    public class  TimeThread implements  Runnable{
        @Override
        public void run() {
            while(true){
                try{
                    Thread.sleep(1000);
                    Message message =new Message();
                    message.what=1;
                    timeHandler.sendMessage(message);
                }catch (Exception e ){
                    e.printStackTrace();
                }
            }
        }
    }
    public void  dismiss(){
        super.dismiss();
    }

}
